package com.yeb.server.exception;

import com.yeb.server.vo.RespBean;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.sql.SQLException;
import java.sql.SQLIntegrityConstraintViolationException;

/**
 * 全局异常处理
 *
 * @Author liushizhong
 * @Date 2021/11/28 17:20
 * @Version 1.0
 */

@RestControllerAdvice
public class GlobalException {

    @ExceptionHandler(SQLException.class)
    @ResponseBody
    public RespBean mySqlException(SQLException e){
        if(e instanceof SQLIntegrityConstraintViolationException){
            return RespBean.error("该数据有关联数据，操作失败！");
        }
        return RespBean.error("数据库异常，操作失败！");
    }
}
